#include "PE_CCMenuItemSprite.h"
#include "CCMenuItem.h"
#include "CCQTSceneDirector.h"
#include "constantManager.h"
PE_CCMenuItemSprite::PE_CCMenuItemSprite(QtVariantPropertyManager *variantManager):PE_CCRGBAProtocol(variantManager)
{
	QDir  dir(ConstantManager::sharedManager()->getResourcePath());
	QStringList filter ;
	filter << "*.png";
	m_fileNameList = dir.entryList(filter);
	m_fileNameList.insert(0, "default");

}

QtProperty *PE_CCMenuItemSprite::initUI()
{
	QtVariantProperty * item = addProperty(QtVariantPropertyManager::enumTypeId() , "NormalSprite");
	item->setAttribute("enumNames" , m_fileNameList);

	addProperty(QVariant::Color , "NormalColor");

	item = addProperty(QtVariantPropertyManager::enumTypeId() , "SelectedSprite");

	item->setAttribute("enumNames" , m_fileNameList);

	addProperty(QVariant::Color , "SelectedColor");

	return m_topItem;
}

void PE_CCMenuItemSprite::refresh()
{
	CCMenuItemSprite * cnode = dynamic_cast<CCMenuItemSprite*>(CCQTSceneDirector::sharedSceneDirector()->CurrentCNode());
	if (cnode == NULL) return;
	int normalimageIndex = m_mapNormalSprite2Imageindex.value((int)cnode);
	int selectedimageIndex = m_mapSelectedSprite2Imageindex.value((int)cnode);
	Property("NormalSprite")->setValue(normalimageIndex);

	Property("NormalColor")->setValue(qcolorFromCNode(dynamic_cast<CCRGBAProtocol*>(cnode->getNormalImage())));

	Property("SelectedColor")->setValue(qcolorFromCNode(dynamic_cast<CCRGBAProtocol*>(cnode->getSelectedImage())));
	Property("SelectedSprite")->setValue(selectedimageIndex);



}

void PE_CCMenuItemSprite::valueChanged(QtProperty *property, QVariant variant)
{
	CCMenuItemSprite * cnode = dynamic_cast<CCMenuItemSprite*>(CCQTSceneDirector::sharedSceneDirector()->CurrentCNode());
	
	if (property == Property("NormalSprite") || property == Property("SelectedSprite"))
	{
		QString fileName = m_fileNameList[variant.value<int>()];
		CCSprite * sprite ;
		if (property == Property("NormalSprite") )
		{
			sprite = dynamic_cast<CCSprite*>(cnode->getNormalImage());
			m_mapNormalSprite2Imageindex.insert((int) cnode,  variant.value<int>());
		}else
		{
			sprite = dynamic_cast<CCSprite*>(cnode->getSelectedImage());
			m_mapSelectedSprite2Imageindex.insert((int) cnode,  variant.value<int>());
		}
		if (fileName != "default")
		{
			CCRect rect = CCRectZero;
			CCTexture2D * pTexture = CCTextureCache::sharedTextureCache()->addImage(fileName.toLocal8Bit());
			rect.size = pTexture->getContentSize();
			sprite->setTexture(pTexture);
			sprite->setTextureRect(rect);
			cnode->setContentSize(cnode->getNormalImage()->getContentSize());
			PEFactory::shareFactory()->getPEditorByName("CCNode")->refresh();
		}
	}

	else if(property == Property("NormalColor"))
	{
		CCSprite * sprite = dynamic_cast<CCSprite*>(cnode->getNormalImage());
		QColor color = variant.value<QColor>();
		sprite->setColor(ccc3(color.red(), color.green() , color.blue()));
		sprite->setOpacity(color.alpha());
	}
	else if(property == Property("SelectedColor"))
	{
		CCSprite * sprite = dynamic_cast<CCSprite*>(cnode->getSelectedImage());
		QColor color = variant.value<QColor>();
		sprite->setColor(ccc3(color.red(), color.green() , color.blue()));
		sprite->setOpacity(color.alpha());
	}


}
